﻿using System;
using Adaptive.Agrona;
using Adaptive.Agrona.Concurrent;
using Unity.Type.BinaryType;

namespace Unity.Type
{
    public class MetadataESerializer : IUnitySerializer<MetadataE>
    {

        private static readonly MessageHeader MessageHeader = new MessageHeader();
        private static readonly MetadataE MetadataEEncoder = new MetadataE();

        private const int MessageLength = MessageHeader.Size + MetadataE.BlockLength;

        private readonly IMutableDirectBuffer _encodeBuffer = new UnsafeBuffer(new byte[MessageLength]);
        
        public byte[] Serialize(MetadataE obj)
        {
            if (obj == null)
            {
                return Array.Empty<byte>();
            }
            
            MetadataEEncoder.WrapForEncodeAndApplyHeader(this._encodeBuffer, 0, MessageHeader);
            MetadataEEncoder.SetCsiId(obj.CsiId);
            MetadataEEncoder.Delimiter = obj.Delimiter;
            MetadataEEncoder.SpecVersion = obj.SpecVersion;
            MetadataEEncoder.SetRandom(obj.GetRandom());
            MetadataEEncoder.SetCounter(obj.GetCounter());
            MetadataEEncoder.Timestamp = obj.Timestamp;
            MetadataEEncoder.Version = obj.Version;
            MetadataEEncoder.ChangerCsiId = obj.ChangerCsiId;
            MetadataEEncoder.EntityType = obj.EntityType;
            MetadataEEncoder.EventType = obj.EventType;
            MetadataEEncoder.FormatVer = obj.FormatVer;
            MetadataEEncoder.VersionDelete = obj.VersionDelete;
            return this._encodeBuffer.ByteArray;
        }


    }
}